import {
  getMyUserHomeInfo,
  setIsOnline,
  getExpertEvaluate,
  getTaAnswerList,
} from "@/static/js/my.js";

export default {
  data() {
    return {
      info: {}, //主要信息
      allCount: 0, //评价条数
      answerList: [],
      majorDirectionList: [],
      //认证专业人士
      majorTypes: "",
      majorStatus: 0, //在线状态
      listStatus: "noMore",
      requestData: {
        page: 1,
        pagesize: 10,
      },
      isRefreshing: false, //界面下拉的状态
      statusShow: false,
      statusList: [{
        name: "在线",
        status: 1,
      },
      {
        name: "离线",
        status: 0,
      },
      ],
      currentPage: 0,
      navList: [{
        name: "我的回答",
      },
      {
        name: "评价",
      },
      ],
      judgeList: [],
    };
  },
  onLoad() {
    this.getExpertEvaluate();
    this.getMyUserHomeInfo();
  },
  onShow() {
    this.getMyUserHomeInfo();
  },
  methods: {
    //设置是否在线
    async selectClick(index) {
      let params = {
        status: index.status,
      };
      setIsOnline(params).then((res) => {
        if (res.code == 1) {
          this.getMyUserHomeInfo();
        }
      });
    },
    //获取评价
    getExpertEvaluate() {
      var data = {
        user_id: uni.getStorageSync("userInfo").id,
        page: this.requestData.page,
        pagesize: this.requestData.pagesize,
      };
      getExpertEvaluate(data).then((res) => {
        if (res.code == 1) {
          this.allCount = res.data.evaluatedatas_count;
          if (this.requestData.page == 1) {
            this.judgeList = [];
          }
          let newList = res.data.evaluatedatas;
          if (newList.length >= this.requestData.pagesize) {
            this.listStatus = "more";
          } else {
            this.listStatus = "noMore";
          }
          this.judgeList.push(...newList);
        }
      });
    },

    //前往回答会话页面
    goInfo(item) {
      uni.navigateTo({
        url: "/quiz/askInfo/index?id=" + item.id,
      });
    },
    navClick(index) {
      this.currentPage = index;
      this.requestData.page = 1;
      if (index == 0) {
        this.answerList = [];
        this.getMyAnswerList(this.info.id);
      } else {
        this.judgeList = [];
        this.getExpertEvaluate();
      }
    },
    close() {
      this.statusShow = false;
    },
    getMyUserHomeInfo() {
      getMyUserHomeInfo().then(({
        data,
        code,
        msg
      }) => {
        if (code == 1) {
          this.answer = data.answer;
          this.majorTypes = data.major_types;
          this.majorStatus = data.majorinfo_status;
          this.info = data.info;
          if (data.info && data.info.major_direction) {
            this.majorDirectionList = data.info.major_direction
              .split(",")
              .slice(0, 4);
          } else {
            this.majorDirectionList = [];
          }

          this.getMyAnswerList(data.info.id);
        }
      });
    },

    //获取我的回答列表
    async getMyAnswerList(id) {
      var data = {
        user_id: id,
        page: this.requestData.page,
        pagesize: this.requestData.pagesize,
      };
      this.listStatus = "loading";
      getTaAnswerList(data).then((res) => {
        if (res.code == 1) {
          let arr = [];
          if (this.requestData.page == 1) {
            this.answerList = [];
          }
          let newList = res.data;
          if (newList.length >= this.requestData.pagesize) {
            this.listStatus = "more";
          } else {
            this.listStatus = "noMore";
          }
          let arr1 = arr.concat(newList);

          arr1.forEach((val) => {
            let obj = {
              answer: JSON.parse(val.first_answer[0].message),
              name: val.quser_nickname,
              time: val.add_time_text,
              avatar: val.quser_avatar,
              content: val.title,
              id: val.id,
            };
            this.answerList.push(obj);
          });
        }
      });
    },

    loadMore() {
      console.log("加载更多");
      if (this.listStatus == "more") {
        this.requestData.page += 1;
        if (this.currentPage == 0) {
          this.getMyAnswerList(this.info.id);
        } else {
          this.getExpertEvaluate();
        }
      }
    },
    goBack() {
      uni.navigateBack({
        delta: 1,
      });
    },
  },
};